﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.BLL.Extend;
using ZX.SYS.Library;
using ZX.DBModule;
using ZX.EHR.Library;
using ZX.Common;

namespace ZX.BLL
{
    public class BSysFunction : BaseBussnies
    {
        public BSysFunction()
        {
            this.entity = new SysFunction();
        }


        public override string CheckData()
        {
            if (string.IsNullOrEmpty((this.entity as Entity).EntityName))
            {
                return "实体名不能为空!";
            }
            return base.CheckData();
        }

        /// <summary>
        /// 返回树型结构
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Condition"></param>
        /// <returns></returns>
        public override IList<T> GetEntitys<T>(string Condition)
        {
            IList<T> list = base.GetEntitys<T>(Condition);
            IList<SysFunction> List = new List<SysFunction>();
            foreach (var en in list)
            {
                SysFunction _en = en as SysFunction;
                List.Add(_en);
            }
            IList<SysFunction> _list = new List<SysFunction>();
            FillList(Guid.Empty, List, _list);
            IList<T> _mlist = new List<T>();
            foreach (var en in _list)
            {
                //object o = en;
                string json = en.ToJSON();
                T t = Common.Globals.JSonToObject<T>(json); //System.Converter;
                _mlist.Add(t);
                //T t = default(T);// (T)this.entity;
            }
            //return _list.To(List(); 
            return _mlist;
        }

        public override IList<BaseEntity> GetEntitysSplitPage(string Condition)
        {
            //return base.GetEntitysSplitPage(Condition);
            return GetEntitysByCondition(Condition);
        }
        public override IList<BaseEntity> GetEntitysByCondition(string Condition)
        {
            IList<SysFunction> list = this.GetEntitys<SysFunction>(Condition);
            IList<SysFunction> _nlist = new List<SysFunction>();
            FillList(Guid.Empty, list, _nlist);
            IList<BaseEntity> _list = new List<BaseEntity>();
            foreach (SysFunction entity in _nlist)
            {
                _list.Add(entity);
            }
            return _list;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="List"></param>
        /// <param name="mList"></param>
        private void FillList(Guid parentId, IList<SysFunction> List, IList<SysFunction> mList)
        {
            foreach (var en in List.Where(p => p.ParentID == parentId))
            {
                if (en.ParentID != Guid.Empty)
                {
                    var _p = List.Where(p => p.FunctionId == en.ParentID).First();
                    en.Level = _p.Level + 1;
                    string str = "";

                    for (int i = 0; i < en.Level; i++)
                    {
                        str = str + ". ";
                    }
                    en.Name = str + en.Name;
                }
                else
                {
                    en.Level = 0;
                }
                mList.Add(en);
                FillList(en.FunctionId, List, mList);            
            }
        }


        /// <summary>
        /// 创建TreeCode
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public string CreateNewTreeCode(Guid parentID)
        {
            SysFunction pDept = this.GetEntityById(parentID.ToString()) as SysFunction;
            IList<SysFunction> deptList = this.GetEntitys<SysFunction>("ParentID='" + parentID + "'");
            if (deptList == null)
            {
                return pDept.FunctionPath + "-001";
            }
            else
            {
                int bDept = deptList.Max(p => p._PathInt);
                if (bDept == 0)
                {
                    return pDept.FunctionPath + "-001";
                }
                string _lastCode = (bDept + 1).ToString("D3");
                return pDept.FunctionPath + "-" + _lastCode;
            }
        }



        /// <summary>
        /// 批量更新部门树编码
        /// </summary>
        /// <param name="functionID">部门ID</param>
        /// <returns></returns>
        public bool BatchUpdateDeptTreeCode(Guid functionID)
        {
            IList<SysFunction> _list =GetEntitys<SysFunction>("FucntionID='"+functionID+"'");
            if (_list == null)
                return false;
            // SysFunction _entity = GetEntityById<SysFunction>(DeptID);
            //_list.Add(_entity);
            foreach (var entity in _list)
            {
                if (entity.FunctionId != functionID)
                {
                    entity.FunctionPath = null;
                    this.Update(entity);
                }
            }
            BatchUpdateDeptTreeCode(functionID, _list);
            return true;
        }

        /// <summary>
        /// 批量更新部门树编码
        /// </summary>
        /// <param name="DeptID">部门ID</param>
        /// <param name="list">要更新部门列表</param>
        /// <returns></returns>
        public bool BatchUpdateDeptTreeCode(Guid DeptID, IList<SysFunction> list)
        {
            if (list == null)
            {
                return false;
            }
            var _list = list.Where(p => p.ParentID == DeptID).OrderBy(p => p.ShowOrder);
            if (_list == null)
            {
                return false;
            }
            foreach (var entity in _list)
            {
                entity.FunctionPath = CreateNewTreeCode(DeptID);
                this.Update(entity);
                BatchUpdateDeptTreeCode(entity.FunctionId, list);
            }
            return true;
        }

    }
}
